package com.bootdo.cash.controller;

import com.bootdo.cash.domain.CashDO;
import com.bootdo.cash.service.CashService;
import com.bootdo.cash.service.ChargeService;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.config.Constant;
import com.bootdo.common.redis.lock.CommonRedisLock;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * @Desc 充值
 * @date 2019/12/24
 *
 */
@Controller
@RequestMapping("/cash/charge")
public class ChargeController {

    private final String PREFIX = "cash/charge";
    @Autowired
    private ChargeService chargeService;
    @Autowired
    private CashService cashService;

    @Autowired
    protected StringRedisTemplate stringRedisTemplate;

    @GetMapping()
    @RequiresPermissions("cash:charge:list")
    String cashWithd(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("cash:charge:list")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<CashDO> list = chargeService.list(query);
        int total = chargeService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @RequiresPermissions("cash:charge:list")
    @Log("获取发起人备注")
    @PostMapping("/findUser")
    @ResponseBody
    R findUser(Long userId) {
        String name = cashService.findUser(userId);
        return R.ok(name);
    }

    @RequiresPermissions("cash:charge:recharge")
    @Log("充值校验")
    @PostMapping("/recharge/check")
    @ResponseBody
    R rechargeCheck(@RequestParam Long id){
        CommonRedisLock commonLock = new CommonRedisLock(stringRedisTemplate, Constant.RECHARGE_CHECK + id);
        try {
            if (commonLock.tryLock(3)) {
                CashDO cash = cashService.get(id);
                if (0 == cash.getStatus().intValue()){
                    cash.setStatus(1);
                    if (cashService.update(cash) > 0) {
                        return R.ok();
                    }
                }
            }
        } catch (Exception e) {
            return R.error();
        } finally {
            commonLock.unlock();
        }
        return R.error();
    }

    @RequiresPermissions("cash:charge:recharge")
    @Log("充值")
    @GetMapping("/recharge/{id}")
    String recharge(Model model, @PathVariable("id") Long id){
        CashDO cash = chargeService.get(id);
        model.addAttribute("userId", id);
        model.addAttribute("user", cash);
        long cashMoney = Math.round(cash.getCashMoney() * 100);
        long lockingMoney = Math.round(cash.getLockingMoney() * 100);
        long actualMoney = Math.round(cash.getActualArrival() * 100);
        DecimalFormat df = new DecimalFormat("0.00");
        model.addAttribute("maxMoney",  Double.valueOf(df.format((float)(cashMoney - lockingMoney - actualMoney)/100)));
        return "cash/cash/recharge";
    }
}
